import { message } from 'antd';
import { getWelcomeMsg, setWelcomeMsg } from '@/services/mySettings';
export default {
  namespace: 'welcome',
  state: { msg: {} },
  effects: {
    *getWelcome({ payload }, { call, put }) {
      const res = yield call(getWelcomeMsg, payload);
      if (+res.code === 0) {
        console.log('获取欢迎语：', res);
        yield put({
          type: 'setData',
          payload: {
            msg: res.data,
          },
        });
      }
    },
    *setWelcome({ payload }, { call, put }) {
      try {
        const res = yield call(setWelcomeMsg, payload);
        console.log('提交结果：', res);
        if (+res.code === 0) {
          message.success('保存成功！');
        } else {
          message.error(res.message);
        }
      } catch (e) {
        console.log(e);
        message.error('保存失败，请稍后再试');
      }
    },
  },
  reducers: {
    setData(state, { payload }) {
      const newState = {
        ...state,
        ...payload,
      };
      return newState;
    },
  },
};
